<template>
  <div>
    <el-button @click="show_dialog">新增</el-button>

    <el-table :data="hos.list" style="width: 100%">
      <el-table-column align="center" prop="name" label="医院名称">
      </el-table-column>
      <el-table-column align="center" prop="introduce" label="医院简介">
      </el-table-column>
      <!-- <el-table-column prop="content" label="内容">
        <template v-slot="scope">
          <div v-html="scope.row.content"></div>
        </template>
      </el-table-column> -->
      <el-table-column align="center" prop="address" label="地址">
      </el-table-column>
      <el-table-column align="center" prop="flags" label="标记">
      </el-table-column>
      <el-table-column align="center" prop="longitude" label="经度">
      </el-table-column>
      <el-table-column align="center" prop="latitude" label="纬度">
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template v-slot="scope">
          <el-button
            style="margin-right: 5px; cursor: pointer"
            @click="open(scope.row.id)"
            >删除</el-button
          >
          <el-button style="cursor: pointer" @click="edit_hos(scope.row)"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页按钮 -->
    <el-pagination
      layout="prev, pager, next"
      :page-size="obj.pageSize"
      :current-page="obj.page"
      @current-change="change_"
      :total="hos.total"
    >
    </el-pagination>
    <!-- 模态框 -->
    <el-dialog :title="title" :visible.sync="dialog_hos" width="70%">
      <el-form :model="form_hos">
        <el-form-item label="医院名称" :label-width="formLabelWidth_hos">
          <el-input v-model="form_hos.name"></el-input>
        </el-form-item>
        <el-form-item label="医院简介" :label-width="formLabelWidth_hos">
          <el-input v-model="form_hos.introduce"></el-input>
          <!-- <el-input
            type="textarea"
            v-model="form_hos.content"
            show-password
          ></el-input> -->
        </el-form-item>
        <el-form-item label="省市区" :label-width="formLabelWidth_hos">
          <el-cascader
            v-model="form_hos.pca"
            :options="pca"
            :props="{ expandTrigger: 'hover', label: 'name', value: 'name' }"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" :label-width="formLabelWidth_hos">
          <el-input v-model="form_hos.address"></el-input>
        </el-form-item>
        <el-form-item label="标记" :label-width="formLabelWidth_hos">
          <el-input v-model="form_hos.flags"></el-input>
        </el-form-item>
        <el-form-item label="经度" :label-width="formLabelWidth_hos">
          <el-input disabled v-model="form_hos.longitude"></el-input>
        </el-form-item>
        <el-form-item label="纬度" :label-width="formLabelWidth_hos">
          <el-input disabled v-model="form_hos.latitude"></el-input>
        </el-form-item>
        <el-form-item label="选择经纬度" :label-width="formLabelWidth_hos">
          <el-button @click="show_map">打开地图选择</el-button>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog_hos = false">取 消</el-button>
        <el-button type="primary" @click="save_hos">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 显示地图的模态框 -->
    <el-dialog title="请点击选择" :visible.sync="dialog_map" width="70%">
      <div id="Map" style="height: 400px"></div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialog_map = false">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapActions, mapState } from "vuex";
import pca from "../../assets/pca.json";
export default {
  computed: {
    ...mapState("hos", ["hos"]),
  },
  // 监听器
  // 当监听的变量发生变化时，执行监听中的代码
  watch: {
    obj: {
      handler: function () {
        this.findHos(this.obj);
      },
      deep: true,
    },
  },
  data() {
    return {
      pca,
      dialog_map: false,
      obj: {
        page: 1,
        pageSize: 10,
      },
      title: "新增",
      dialog_hos: false,
      form_hos: {},
      formLabelWidth_hos: "120px",
      map: {},
      // dialogVisible: false
    };
  },
  created() {
    this.findHos(this.obj);
  },
  methods: {
    ...mapActions("hos", ["findHos", "saveHos", "deleteHos"]),
    open(id) {
      this.$confirm("您将永久删除该文章, 是否继续?", "提示！！！", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.delete_hos(id);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    initMap() {
      this.map = new AMap.Map("Map", {
        resizeEnable: true,
      });
    },
    // 获取经纬度
    showInfoClick(e) {
      this.form_hos.longitude = e.lnglat.getLng();
      this.form_hos.latitude = e.lnglat.getLat();
      console.log(e.lnglat.getLng(), e.lnglat.getLat());
    },
    // 给地图绑定单击事件
    ClickOn() {
      this.map.on("click", this.showInfoClick);
    },
    // 显示地图
    show_map() {
      this.dialog_map = true;
      setTimeout(() => {
        this.initMap();
        this.ClickOn();
      }, 1000);
    },
    show_dialog() {
      this.dialog_hos = true;
      this.form_hos = {};
    },
    delete_hos(id) {
      var obj = {
        id: id,
      };
      this.deleteHos(obj);

      this.findHos(this.obj);

      // this.dialogVisible=false
    },
    edit_hos(val) {
      // 修改模态框的标题
      this.title = "修改";
      // 弹出模态框
      this.dialog_hos = true;
      // 将当前行的数据的放入模态框
      this.form_hos = JSON.parse(JSON.stringify(val));
    },
    change_(value) {
      this.obj.page = value;
      // console.log(this.obj.page);
    },
    save_hos() {
      this.form_hos.province = this.form_hos.pca[0];
      this.form_hos.city = this.form_hos.pca[1];
      this.form_hos.area = this.form_hos.pca[2];
      this.saveHos(this.form_hos).then((res) => {
        this.dialog_hos = false;
        this.findHos(this.obj);
      });
      this.form_hos = {};

      // console.log(this.form_hos);
    },
    handleChange() {},
  },
};
</script>